package com.android.happymall.fragment.member;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.activity.BaseObject;
import com.android.happymall.model.data;
import com.android.happymall.utils.ObjectUtils;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.model.dataCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.MyLog;
import com.android.happymall.view.dialog.MainMsg;
import com.android.happymall.R;
import com.android.happymall.utils.LoginState;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 提现
 */
public class Fragment_Mine_Money_Cash extends BaseLazyFragment implements TextWatcher {


    @BindView(R.id.price_type)
    TextView priceType;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.cash_style)
    TextView cashStyle;
    @BindView(R.id.cash_name)
    EditText cashName;
    @BindView(R.id.cash_num)
    EditText cashNum;
    @BindView(R.id.cash_price)
    EditText cashPrice;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;

    private static final String ARG_PARAM1 = "param1";
    private String param1;
    private boolean networkConnected;


    public Fragment_Mine_Money_Cash() {


    }


    public static Fragment_Mine_Money_Cash newInstance(String price) {
        Fragment_Mine_Money_Cash fragment = new Fragment_Mine_Money_Cash();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, price);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            param1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {

        priceType.setText("可提现总额（元）");
        cashPrice.addTextChangedListener(this);
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {

            price.setText(param1);

            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
        } else {

            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }


    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_money_cash, container, false);
        ButterKnife.bind(this, view);
        mcontext = getActivity();


        return view;
    }

    private String pay_id = "1";

    @OnClick({R.id.cash_style, R.id.cash_post, R.id.cash_record, R.id.click_resetnetwork_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cash_style:

                final MainMsg mdialog = new MainMsg(mcontext);
                mdialog.show(mcontext.getWindow().getDecorView(), 5, null);
                mdialog.setPopListDataClickListener(new MainMsg.OnPopListDataClickListener() {
                    @Override
                    public void onData(String name, String id) {
                        cashStyle.setText(name);
                        pay_id = id;
                        mdialog.close();
                    }
                });
                break;
            case R.id.cash_post:
                String name = cashName.getText().toString().trim();
                String num = cashNum.getText().toString().trim();
                String price = cashPrice.getText().toString().trim();

                if (name.equals("") || num.equals("") || price.equals("")) {
                    ObjectUtils.toast(mcontext, "请将信息填写完整");
                    return;
                }
                CashPost(pay_id, name, num, price);
                break;
            case R.id.cash_record:
                Intent intent_q = new Intent(getActivity(), BaseObject.class);
                intent_q.putExtra("title", "提现记录");
                startActivity(intent_q);
                break;
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                break;
        }

    }


    private void CashPost(String type, String realname, String account, String amount) {


        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERMONEY_CASH;
        MyLog.e("地址", address);
        if (networkConnected) {
            progressLoading.setVisibility(View.VISIBLE);
            HashMap<String, String> header = new HashMap<>();
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            header.put("UID", getid);
            header.put("UPSW", getpd);

            HashMap<String, String> map = new HashMap<>();
            map.put("type", type);//提现方式
            map.put("realname", realname);//姓名
            map.put("account", account);//收款账号
            map.put("amount", amount);//金额


            MyLog.e("提现方式", type);
            MyLog.e("姓名", realname);
            MyLog.e("收款账号", account);
            MyLog.e("金额", amount);


            PostFormBuilder post = OkHttpUtils.post();
            post.url(address)
                    .headers(header)
                    .params(map)
                    .build()
                    .execute(new dataCallback() {

                        @Override
                        public void onError(Call call, Exception e, int id) {

                            progressLoading.setVisibility(View.GONE);

                            MyLog.e("连接错误", "" + e.toString());

                        }

                        @Override
                        public void onResponse(data response, int id) {
                            progressLoading.setVisibility(View.GONE);
                            if (response.getCode() == 0) {


                                ObjectUtils.toast(mcontext, "提现成功");
                                Intent intent_q = new Intent(getActivity(), BaseObject.class);
                                intent_q.putExtra("title", "提现记录");
                                startActivity(intent_q);
                                mcontext.finish();

                            } else {
                                ObjectUtils.toast(mcontext, response.getMsg());


                            }
                        }
                    });
        } else {

            ObjectUtils.toast(mcontext, "网络未连接");


        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String trim = cashPrice.getText().toString().trim();
        double v = Double.parseDouble(param1);
        if (!trim.equals("")) {
            Double can = Double.parseDouble(trim);
            if (can > v) {

                ObjectUtils.toast(mcontext, "已超出可提现金额");
            }
        }
    }

}
